package com.handpay.nocardpay.baselibrary.http;

import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.http.stack.OkHttp3Stack;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.SSLSocketFactory;

import okhttp3.OkHttpClient;

/**
 * @author sxshi
 */
public class HttpQueueManager {
    private static RequestQueue mRequestQueue;
    public static final int DEFAULT_TIME_OUT = 60 * 1000; //default timeout
    private List<Object> tags = new ArrayList<>();
    private HttpQueueManager(Builder builder) {

        OkHttpClient.Builder okhttpBuilder = new OkHttpClient.Builder();
        if (builder.isHttps) {
            if (builder.sslSocketFactory == null) {
                HPLog.d(HPLog.SSX_TAG,"使用项目默认证书");
                okhttpBuilder.sslSocketFactory(builder.defaultSocketFactory);
            } else {
                HPLog.d(HPLog.SSX_TAG,"设置了自定义证书");
                okhttpBuilder.sslSocketFactory(builder.sslSocketFactory);
            }
        }
        mRequestQueue = Volley.newRequestQueue(builder.mContext, new OkHttp3Stack(okhttpBuilder.build()));
    }

    /**
     * add request
     *
     * @param request
     * @param tag
     */
    public void addRequest(Request request, Object tag) {
        if (tag != null) {
            request.setTag(tag);
            tags.add(tag);
        }
        request.setRetryPolicy(new CustomRetryPolicy(DEFAULT_TIME_OUT));
        if (mRequestQueue != null) {
            mRequestQueue.add(request);
        } else {
            throw new IllegalArgumentException("RequestQueue is not initialized");
        }
    }


    /**
     * cancelRequest
     *
     * @param tag
     */
    public void cancelRequest(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        } else {
            throw new IllegalArgumentException("RequestQueue is not initialized");
        }
    }

    public static class Builder {
        private Context mContext;
        private boolean isHttps;

        //自定义证书
        private SSLSocketFactory sslSocketFactory;

        //默认项目使用双向认证
        private SSLSocketFactory defaultSocketFactory;

        public Builder(Context mContext) {
            this.mContext = mContext;
            InputStream p12Keystore = mContext.getResources().openRawResource(R.raw.safesftclient);
            InputStream trustKeyStore = mContext.getResources().openRawResource(R.raw.safesftserver);
            defaultSocketFactory = SSLCustomSocketFactory.getSocketFactory(p12Keystore, NoCardPayConfig.P12KEY, trustKeyStore);
        }

        public Builder isHttps(boolean isHttps) {
            this.isHttps = isHttps;
            return this;
        }

        public Builder setSSLSocketFactory(SSLSocketFactory sslSocketFactory) {
            this.sslSocketFactory = sslSocketFactory;
            return this;
        }

        public HttpQueueManager build() {
            return new HttpQueueManager(this);
        }
    }
}
